﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace MvcBoost.EntityFramework
{
    internal class IsolatedContextManager<TContext> : IDbContextManager<TContext>
        where TContext : DbContext
    {

        public IsolatedContextManager(TContext context, bool isSharedContext)
        {
            if (context == null) throw new ArgumentNullException("context");
            this.Context = context;
            this.IsSharedContext = isSharedContext;
        }
        public TContext CreateContext()
        {
            // Return the passed context
            return this.Context;
        }

        public void RemoveContext()
        {
            // If Context is not shared and not null, dispose            
            if ((!IsSharedContext) && (this.Context != null))
            {
                this.Context.Dispose();
                this.Context = null;
            }
        }

        public bool IsSharedContext { get; private set; }

        public TContext Context { get; private set; }
    }
}
